package com.gehj.androidlibs.fragments;

import android.content.Intent;
import android.app.Fragment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;

import com.gehj.alluitls.androidUtils.LogUtil;
import com.gehj.androidlibs.R;
import com.gehj.uhttp.GlobleURL;
import com.gehj.uhttp.OkhttpUtils;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ReadingFragment extends Fragment  implements LifecycleOwner  {
    private static final String TAG = "ReadingFragment";
    private View view;
    private TextView textView;
    LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);
    private Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    if (null == getActivity()) return;
                    LogUtil.e(TAG, getActivity().getLocalClassName());
                    String s = (String) msg.obj;
                    textView.setText(s);
                    break;
            }


        }
    };


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLifecycle().addObserver(OkhttpUtils.getInstance(TAG));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.activity_main_handler, null);
        initView();
        setListviewData();
        return view;
    }


   @Override
    public void onHiddenChanged(boolean hidden) {
       super.onHiddenChanged(hidden);
       setListviewData();
    }

    private void setListviewData() {
        // String baseurl = GlobalContsts.BASEURL_NoticeURL;
        //String url = baseurl + "{'appid':'101','userid':'"+ userid+"','isreadid':'100009100002000001'}";
        OkhttpUtils.get_data(GlobleURL.banner, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json = response.body().string();
                Message message = Message.obtain();
                message.what = 0;
                message.obj = json;
                handler.sendMessage(message);
            }
        });
    }

    private void initView() {
        textView = (TextView) view.findViewById(R.id.text);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        OkhttpUtils.cancelTagCall(TAG);
        handler.removeCallbacksAndMessages(null);
    }


    @NonNull
    @NotNull
    @Override
    public Lifecycle getLifecycle() {
        return mLifecycleRegistry;
    }
}
